package com.school.basic.service.impl;

import com.school.basic.entity.AreaInfo;
import com.school.basic.entity.SchoolInfo;
import com.school.basic.repository.AreaInfoRepository;
import com.school.basic.service.AreaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class AreaInfoServiceImp implements AreaInfoService {
    @Autowired
    AreaInfoRepository areaInfoRepository;

    /**
     * 增加
     * @param areaInfo
     * @return
     */
    @Override
    public AreaInfo addAreaInfo(AreaInfo areaInfo) {
        areaInfo.setCreateDate(new Date());
        areaInfo.setUpdateDate(new Date());
        return areaInfoRepository.saveAndFlush(areaInfo);
    }

    /**
     * 修改
     * @param areaInfo
     * @return
     */
    @Override
    public AreaInfo updateAreaInfo(AreaInfo areaInfo) {
        areaInfo.setUpdateDate(new Date());
        return areaInfoRepository.saveAndFlush(areaInfo);
    }

    /**
     * 查看详情
     * @param id
     */
    @Override
    public AreaInfo getDetailsById(String id) {
        return areaInfoRepository.findById(Integer.valueOf(id)).get();
    }

    /**
     * 区域信息状态变更
     * @param id,status
     */
    @Override
    public void updateStatus(String id,Integer status) {
        AreaInfo areaInfo;
        try {
            areaInfo = areaInfoRepository.findById(Integer.valueOf(id)).get();
            areaInfo.setStatus(status);
            areaInfoRepository.saveAndFlush(areaInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteAreaInfo(String id) {
        AreaInfo areaInfo;
        try {
            areaInfo = areaInfoRepository.findById(Integer.valueOf(id)).get();
            areaInfoRepository.delete(areaInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量删除
     * @param idList
     */
    @Override
    public void deleteAreaInfoList(String idList) {
        String[] split = idList.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        areaInfoRepository.deleteAreaInfosByIdIn(list);
    }

    /**
     * 根据条件分页查询
     * @param areaInfo,page,size
     */
    @Override
    public Page<AreaInfo> specificPageQueryAreaInfoList(AreaInfo areaInfo, int page, int size) {
        Specification querySpecification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(areaInfo != null)
                {
                    Integer status = areaInfo.getStatus();
                    String name = areaInfo.getName();
                    if (status != null)
                    {
                        predicates.add(criteriaBuilder.equal(root.get("status").as(Integer.class),status));
                    }
                    if (!StringUtils.isEmpty(name))
                    {
                        predicates.add(criteriaBuilder.like(root.get("name").as(String.class),'%' + name + '%'));
                    }
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        PageRequest pageRequest = PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC,"updateDate"));
        return areaInfoRepository.findAll(querySpecification,pageRequest);
    }

    /**
     * 查询所有
     * @param
     */
    @Override
    public List<AreaInfo> findAll() {
        return areaInfoRepository.findAll();
    }

    @Override
    public AreaInfo findAreaByAccountAndPassword(Integer account, String password) {
        AreaInfo areaInfo = new AreaInfo();
        areaInfo.setAccount(account);
        areaInfo.setPassword(password);
        Optional<AreaInfo> one = areaInfoRepository.findOne(Example.of(areaInfo));
        areaInfo = null;
        return one.orElse(null);
    }

}
